# Maintainer: Balló György <ballogyor+arch at gmail dot com>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Unknown47 <unknown47r@gmail.com>
# Contributor: Angel Velasquez <angvp@archlinux.org>
# Contributor: Juergen Hoetzel <juergen@archlinux.org>

pkgbase=libfm
pkgname=(libfm libfm-extra libfm-gtk2 libfm-gtk3)
pkgver=1.3.2
pkgrel=1
pkgdesc='Library for file management'
url='https://lxde.org/'
arch=('loong64' 'x86_64')
license=('GPL')
depends=('gtk2' 'gtk3' 'libexif' 'menu-cache')
makedepends=('intltool' 'gtk-doc')
source=(https://downloads.sourceforge.net/pcmanfm/libfm-$pkgver.tar.xz)
sha256sums=('a5042630304cf8e5d8cff9d565c6bd546f228b48c960153ed366a34e87cad1e5')

prepare() {
  cd $pkgbase-$pkgver
  # Update xarchiver option
  # https://github.com/lxde/libfm/issues/35
  sed -i 's/create=xarchiver --add-to %F/create=xarchiver --compress %F/' data/archivers.list

  autoreconf -fi
}

build() {
  # GTK+ 2 version
  [ -d gtk2 ] || cp -r $pkgbase-$pkgver gtk2
  cd gtk2
  ./configure --prefix=/usr \
    --sysconfdir=/etc \
    --with-gnu-ld \
    --enable-gtk-doc

  #https://bugzilla.gnome.org/show_bug.cgi?id=656231
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool

  make

  cd "$srcdir"
  # GTK+ 3 version
  [ -d gtk3 ] || cp -r $pkgbase-$pkgver gtk3
  cd gtk3
  ./configure --prefix=/usr \
    --sysconfdir=/etc \
    --with-gnu-ld \
    --with-gtk=3

  #https://bugzilla.gnome.org/show_bug.cgi?id=656231
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool

  make
}

package_libfm() {
  depends=('libexif' "libfm-extra=$pkgver" 'menu-cache')
  
  cd gtk2
  make DESTDIR="$pkgdir" install

  # Temporary fix to FS#32361
  rm -rf "$pkgdir"/usr/include/libfm
  mv "$pkgdir"/usr/include/libfm-1.0/ "$pkgdir"/usr/include/libfm

  # Split libfm-extra
  [[ -d "$srcdir/libfm-extra" ]] && rm -r "$srcdir/libfm-extra/"
  mkdir "$srcdir"/libfm-extra
  mv "$pkgdir"/usr/include/libfm/fm-{extra,version,xml-file}.h \
     "$pkgdir"/usr/lib/libfm-extra.* \
     "$pkgdir"/usr/lib/pkgconfig/libfm-extra.pc \
     "$srcdir/libfm-extra/"

  # Split libfm-gtk2
  [[ -d "$srcdir/libfm-gtk2" ]] && rm -r "$srcdir/libfm-gtk2/"
  mkdir "$srcdir"/libfm-gtk2
  mv "$pkgdir/usr/bin" \
     "$pkgdir"/usr/lib/libfm-gtk.* \
     "$pkgdir"/usr/lib/libfm/modules/gtk-* \
     "$pkgdir/usr/lib/pkgconfig/libfm-gtk.pc" \
     "$pkgdir/usr/share/applications" \
     "$pkgdir/usr/share/man" \
     "$srcdir/libfm-gtk2/"
}

package_libfm-extra() {
  pkgdesc="Extra library for file management" 
  depends=('glib2')

  cd "$srcdir"/libfm-extra
  mkdir -p "$pkgdir"/usr/{include/libfm,lib/pkgconfig}
  mv *.h "$pkgdir/usr/include/libfm"
  mv libfm-extra.so* "$pkgdir/usr/lib"
  mv libfm-extra.pc "$pkgdir/usr/lib/pkgconfig"
}

package_libfm-gtk2() {
  pkgdesc='GTK+ 2 library for file management'
  depends=('gtk2' 'menu-cache' "libfm=$pkgver")
  replaces=('lxshortcut')
  provides=('lxshortcut')
  conflicts=('lxshortcut')

  cd "$srcdir"/libfm-gtk2
  mkdir -p "$pkgdir"/usr/{lib/{libfm/modules,pkgconfig},share}
  mv bin "$pkgdir/usr"
  mv libfm-gtk.so* "$pkgdir"/usr/lib
  mv gtk-* "$pkgdir"/usr/lib/libfm/modules
  mv libfm-gtk.pc "$pkgdir/usr/lib/pkgconfig"
  mv applications "$pkgdir/usr/share"
  mv man "$pkgdir/usr/share"
}

package_libfm-gtk3() {
  pkgdesc='GTK+ 3 library for file management'
  depends=('gtk3' 'menu-cache' "libfm=$pkgver")
  conflicts=('libfm-gtk2' 'lxshortcut')

  cd gtk3
  make DESTDIR="$srcdir"/libfm-gtk3 install

  cd "$srcdir"/libfm-gtk3
  mkdir -p "$pkgdir"/usr/{lib/{libfm/modules,pkgconfig},share}
  mv usr/bin "$pkgdir/usr"
  mv usr/lib/libfm-gtk3.so* "$pkgdir"/usr/lib
  mv usr/lib/libfm/modules/gtk-* "$pkgdir"/usr/lib/libfm/modules
  mv usr/lib/pkgconfig/libfm-gtk3.pc "$pkgdir/usr/lib/pkgconfig"
  mv usr/share/applications "$pkgdir/usr/share"
  mv usr/share/man "$pkgdir/usr/share"
}
